﻿#include "hotelmanage.h"
#include "ui_hotelmanage.h"
//#include <QHeaderView>
//#include <QAbstractItemView>
#include <QMessageBox>
#include "globalvars.h"
#include "execsql.h"
#include <QDebug>

HotelManage::HotelManage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HotelManage)
{
    ui->setupUi(this);

    m_page = 1;
    m_pageCount = 1;
    m_change = false;

    QVector<QString> vector(3,"");
    qDebug() << "vector.size = " <<vector.size();
    for(int i = 0; i < 4; i++)
    {
        m_oneMoreCondition.append(vector);
    }

    updateHotelInfoShow();
    initWindowsUnits();
}

HotelManage::~HotelManage()
{
    delete ui;
}


void HotelManage::initWindowsUnits()
{
    ui->le_condition->setEnabled(false);
    ui->pb_save->setEnabled(false);

    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不能对表格内容进行修改
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}
void HotelManage::updateHotelInfo()
{
    ExecSQL::searchAllHotelInfos();
    updateHotelInfoShow();
}
void HotelManage::updateHotelInfoShow()
{
    //更新页数，更新内容
    int size = GlobalVars::g_hotelInfoList->size();
    int sizeTemp = size;

    m_pageCount = 1;
//    qDebug() << "size = " << sizeTemp;
    while (sizeTemp > 10)
    {
        m_pageCount += (sizeTemp / 10);
        sizeTemp /= 10;
    }

    if (m_page > m_pageCount)
    {
        m_page = m_pageCount;
    }
    ui->tableWidget->clearContents();
    ui->lb_pageShow->setText(QString::number(m_page) + "/" + QString::number(m_pageCount));

    int index = (m_page-1) * 10;
    if (size >= m_page * 10)
    {
        size = m_page * 10;
    }
    else
    {
        size %= 10;
        size += index;
    }

    QString state;
    for(int i = 0; index < size; index++,i++)
    {
        if (GlobalVars::g_hotelInfoList->at(index).getState() == "已审核")
        {
            QTableWidgetItem *item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getHotel());
            ui->tableWidget->setItem(i, 0, item);
            item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getName());
            ui->tableWidget->setItem(i, 1, item);
            item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getCity());
            ui->tableWidget->setItem(i, 2, item);
            item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getLocation());
            ui->tableWidget->setItem(i, 3 ,item);
            item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getType());
            ui->tableWidget->setItem(i, 4, item);

            item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getStar());
            ui->tableWidget->setItem(i, 5, item);
            item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getMark());
            ui->tableWidget->setItem(i, 6, item);
            item = new QTableWidgetItem(QString::number(GlobalVars::g_hotelInfoList->at(index).getCommentCount()));
            ui->tableWidget->setItem(i, 7, item);
            item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getUser());
            ui->tableWidget->setItem(i, 8, item);
            item = new QTableWidgetItem(GlobalVars::g_hotelInfoList->at(index).getPhone());
            ui->tableWidget->setItem(i, 9, item);
        }
    }
}

void HotelManage::on_pb_nextPage_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    if (m_page >= m_pageCount)
    {
        return;
    }
    m_page++;
    updateHotelInfoShow();
}
void HotelManage::on_pb_lastPage_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    if(m_page <= 1)
    {
        return;
    }
    m_page--;
    updateHotelInfoShow();
}
void HotelManage::on_pb_firstPage_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    if(m_page == 1)
    {
        return;
    }
    m_page = 1;
    updateHotelInfoShow();
}
void HotelManage::on_pb_endPage_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    if(m_page == m_pageCount)
    {
        return;
    }
    m_page = m_pageCount;
    updateHotelInfoShow();
}
void HotelManage::on_pb_jump_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }
    QString text = ui->le_jumpPage->text();
    int pageCount = text.toInt();
    if  (text ==  "")
    {
        QMessageBox::warning(this,
                                     "警告",
                                     "请输入页数",
                                     QMessageBox::Yes,
                                     QMessageBox::Yes);
        return;
    }
    if (pageCount < 1 || pageCount > m_pageCount)
    {
        QMessageBox::warning(this,
                                     "警告",
                                     "请输入正确的页数范围",
                                     QMessageBox::Yes,
                                     QMessageBox::Yes);
        return;
    }
    if (pageCount == m_page)
    {
        return;
    }
    m_page = pageCount;
    updateHotelInfoShow();
}

void HotelManage::on_pb_search_clicked()
{
    if (m_change && m_modifyList.size() > 0)
    {
        on_pb_save_clicked();
    }

    int count = 0;
    QVector<int> conditionList;
    QStringList sql;
    for (int i = 0; i < 4; i++)
    {
        if (!m_oneMoreCondition[i][0].isEmpty())
        {
            count++;
            conditionList.append(i);
        }
    }

    if (count == 0)
    {
        ExecSQL::searchAllHotelInfos();
    }
    else if (count == 1)
    {
        switch (conditionList[0])///判断哪个下拉键选中条件
        {
            case H_Type:
                ExecSQL::searchHotelInfoForType(m_oneMoreCondition[H_Type][1]); break;
            case H_Star:
                ExecSQL::searchHotelInfoForStar(m_oneMoreCondition[H_Star][1]); break;
            case H_Mark:
                if (m_oneMoreCondition[H_Mark][1].toInt() == HML_4_0)
                {
                    ExecSQL::searchHotelInfoForLesserMark(m_oneMoreCondition[H_Mark][2]);
                }
                else
                {
                    ExecSQL::searchHotelInfoForGreaterMark(m_oneMoreCondition[H_Mark][2]);
                }  break;
            case H_Select:
                {
                    QString condition = ui->le_condition->text();
                    m_oneMoreCondition[H_Select][2] = condition;
                    switch (m_oneMoreCondition[H_Select][1].toInt())
                    {
                        case H_Sel_Number:
                            ExecSQL::searchHotelInfoForNumber(condition); break;
                        case H_Sel_Name:
                            ExecSQL::searchHotelInfoForName(condition); break;
                        case H_Sel_City:
                            ExecSQL::searchHotelInfoForCity(condition); break;
                        case H_Sel_User:
                            ExecSQL::searchHotelInfoForUser(condition); break;
                        case H_Sel_Location:
                            ExecSQL::searchHotelInfoForLocation(condition); break;
                        default:  break;
                    }
                } break;
            default:  break;
        }
    }
    else{///多个条件
        QString condition = ui->le_condition->text();
        m_oneMoreCondition[H_Select][2] = condition;
        ExecSQL::searchHotelInfoForMoreCondition(m_oneMoreCondition);
    }

    updateHotelInfoShow();
}
void HotelManage::on_pb_modify_clicked()
{
    m_change = true;
    ui->pb_save->setEnabled(true);
    ui->pb_modify->setEnabled(false);
    ui->tableWidget->setEditTriggers(QAbstractItemView::DoubleClicked); //不能对表格内容进行修改
}
void HotelManage::on_pb_save_clicked()
{
    m_change = false;
    ui->pb_save->setEnabled(false);
    ui->pb_modify->setEnabled(true);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不能对表格内容进行修改

    //修改数据库/刷新
    for(auto iter : m_modifyList)
    {
        int index = iter.at(1).toInt();
        switch (index)
        {
            case Hotel_User:
                ExecSQL::modifyHotelInfoForUser(iter.at(0),iter.at(2));
                break;
            case Hotel_Name:
                ExecSQL::modifyHotelInfoForName(iter.at(0),iter.at(2));
                break;
            case Hotel_City:
                ExecSQL::modifyHotelInfoForCity(iter.at(0),iter.at(2));
                break;
            case Hotel_Location:
                ExecSQL::modifyHotelInfoForLocation(iter.at(0),iter.at(2));
                break;
            case Hotel_Type:
                ExecSQL::modifyHotelInfoForType(iter.at(0),iter.at(2));
                break;

            case Hotel_Star:
                ExecSQL::modifyHotelInfoForStar(iter.at(0),iter.at(2));
                break;
            case Hotel_Mark:
                ExecSQL::modifyHotelInfoForMark(iter.at(0),iter.at(2));
                break;
            case Hotel_CommentCount:
                ExecSQL::modifyHotelInfoForCommentCount(iter.at(0),iter.at(2));
                break;
            case Hotel_Phone:
                ExecSQL::modifyHotelInfoForPhone(iter.at(0),iter.at(2));
                break;
            default: break;
        }
    }
    on_pb_search_clicked();
}
void HotelManage::on_pb_delete_clicked()
{
    QItemSelectionModel *selections = ui->tableWidget->selectionModel(); //返回当前的选择模式
    QModelIndexList selectedsList = selections->selectedRows(); //返回所有选定的模型项目所在行的列表
    if (selectedsList.size() < 1)
    {
        QMessageBox::warning(this,
                                 "错误",
                                 "请选择要删除的行",
                                 QMessageBox::Ok,
                                 QMessageBox::Ok);
        return;
    }

    int result = QMessageBox::warning(this,
                         "警告",
                         "是否确定删除此用户信息！！！",
                         QMessageBox::Yes | QMessageBox::Cancel,
                         QMessageBox::Cancel);

    if (result == QMessageBox::Yes)
    {
        for (int i = 0; i < selectedsList.count(); i++)//数据库进行删除
        {
            int row = selectedsList.at(i).row();
            ExecSQL::deleteHotelInfo(ui->tableWidget->item(row,0)->text());
        }

        on_pb_search_clicked();
    }
}

void HotelManage::on_tableWidget_cellChanged(int row, int column)
{
    if (!m_change)
    {
        return;
    }
    if (column == 0)
    {
        QMessageBox::warning(this,
                                     "警告",
                                     "不能修改酒店编号！！！",
                                     QMessageBox::Yes,
                                     QMessageBox::Yes);
        on_pb_save_clicked();
        updateHotelInfoShow();
        return;
    }

    QVector<QString> modify;//id + column + text
    modify.push_back(ui->tableWidget->item(row,0)->text());
    modify.push_back(QString::number(column));
    modify.push_back(ui->tableWidget->item(row,column)->text());
    m_modifyList.push_back(modify);

    qDebug() << "on_tableWidget_cellChanged(int row, int column)";
    qDebug() << "modify = " << modify;
    qDebug() << "m_modifyList = "  << m_modifyList;
}
void HotelManage::on_cb_type_currentIndexChanged(int index)
{
    m_page = 1;
    if (index != 0)
    {
        QVector<QString> vector;
        vector.append(QString::number(H_Type));
        vector.append(ui->cb_type->currentText());
        m_oneMoreCondition[H_Type] = vector;
    }
    else
    {
        m_oneMoreCondition[H_Type][0] = "";
    }
}
void HotelManage::on_cb_star_currentIndexChanged(int index)
{
    m_page = 1;
    if (index != 0)
    {
        QVector<QString> vector;
        vector.append(QString::number(H_Star));
        vector.append(ui->cb_star->currentText());
        m_oneMoreCondition[H_Star] = vector;
    }
    else
    {
        m_oneMoreCondition[H_Star][0] = "";
    }
}
void HotelManage::on_cb_mark_currentIndexChanged(int index)
{
    m_page = 1;
    if (index != 0)
    {
        QVector<QString> vector;
        QString mark;
        vector.append(QString::number(H_Mark));
        vector.append(QString::number(index));
        switch (index)
        {
            case HMG_5_0: mark = "5.0"; break;
            case HMG_4_8:mark = "4.8"; break;
            case HMG_4_5:mark = "4.5"; break;
            case HMG_4_2:mark = "4.2"; break;
            case HMG_4_0:mark = "4.0"; break;
            case HML_4_0:mark =  "4.0"; break;
            default:            break;
        }
        vector.append(mark);
        m_oneMoreCondition[H_Mark] = vector;
    }
    else
    {
        m_oneMoreCondition[H_Mark][0] = "";
    }
}
void HotelManage::on_cb_select_currentIndexChanged(int index)
{
    m_page = 1;
    if (index == 0)
    {
        ui->le_condition->setText("");
        ui->le_condition->setEnabled(false);
        m_oneMoreCondition[H_Select][0] = "";
    }
    else
    {
        ui->le_condition->setEnabled(true);
        QVector<QString> vector;
        vector.append(QString::number(H_Select));
        vector.append(QString::number(index));
        vector.append(ui->cb_select->currentText());
        m_oneMoreCondition[H_Select] = vector;
    }
}
